package com.ctgu.tengyun.controller.user;

import com.ctgu.tengyun.bean.TokenBean;
import com.ctgu.tengyun.bean.errorcode.UserErrorCode;
import com.ctgu.tengyun.bean.request.SimpleStringRequest;
import com.ctgu.tengyun.bean.request.UserLoginRequest;
import com.ctgu.tengyun.bean.response.TokenValidateResponse;
import com.ctgu.tengyun.bean.response.UserLoginResponse;
import com.ctgu.tengyun.domain.SysUser;
import com.ctgu.tengyun.exception.ErrorCodeException;
import com.ctgu.tengyun.service.user.SysUserService;
import com.ctgu.tengyun.util.MD5Utils;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.TokenUtil;
import com.ctgu.tengyun.util.Validator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by liaoweizhi on 2019/8/15.
 */
@RestController
@RequestMapping(value = "/v1/user/auth/")
public class AuthController {

    @Value("${token.expire.seconds}")
    private Long tokenExpireSeconds;

    @Resource
    private SysUserService sysUserService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public UserLoginResponse login(@RequestBody UserLoginRequest request) {
        String username = request.getUserName();
        String password = request.getPassword();

        Validator.assertHasFields(!StringUtils.isEmpty(username), "userName is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(password), "password is required.");

        SysUser sysUser = sysUserService.selectByUserName(username);
        if (null == sysUser) {
            throw new ErrorCodeException(UserErrorCode.api_user_info_not_init, "api_user_info_not_init");
        }
        boolean isPasswordValid = sysUser.getPassword().equals(MD5Utils.MD5EncodeUTF8(password));
        if (!isPasswordValid) {
            throw new ErrorCodeException(UserErrorCode.name_or_pass_error, "name_or_pass_error");
        }
        String token = TokenUtil.generatorEncryptedToken(sysUser.getId(), sysUser.getRoleId(), tokenExpireSeconds);

        UserLoginResponse response = new UserLoginResponse();
        response.setUserId(sysUser.getId());
        response.setToken(token);

        return response;
    }

    @RequestMapping(value = "validate", method = RequestMethod.POST)
    public TokenValidateResponse validate(@RequestBody SimpleStringRequest request) {
        Validator.assertHasFields(!StringUtils.isEmpty(request.getData()), "data token is required.");

        TokenBean tokenBean = TokenUtil.validatePassportToken(request.getData());
        if (null == tokenBean) {
            throw new ErrorCodeException(UserErrorCode.token_error, "token error");
        }

        return new TokenValidateResponse(tokenBean.getUserId(), tokenBean.getRoleId(), tokenBean.getExpireTime());
    }

}
